/****************************************************
	文件：ResDataComponent.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2021/1/3 12:29:5
	功能：资源数据显示组件
*****************************************************/

using HTFW.Utility.Global;
using UnityEngine;

namespace HTFW.Res.Helper
{
    public class ResDataComponent : MonoBehaviour
    {
        public const float WIDTH = 350f;
        public const float HEIGHT = 25f;

        GUIStyle mStyle;

        public GUIStyle Style
        {
            get
            {
                if (mStyle == null)
                {
                    mStyle = new GUIStyle();
                    mStyle.fontSize = 28;
                    mStyle.normal.textColor = Color.black;
                }

                return mStyle;
            }
        }

        private void OnGUI()
        {
#if UNITY_EDITOR
            Graw();
#endif
        }

        void Graw()
        {
            float x = 5f;
            float y = 1f;

            if (ResConfig.Instance.Cfg.IsNullOrEmpty()) return;
            foreach (var item in ResConfig.Instance.Cfg)
            {
                if (GrawResRef(x, y, item.Value))
                    y += HEIGHT;
            }
        }

        bool GrawResRef(float x, float y, ResData resData)
        {
            if (resData == null || resData.RefCount <= 0) return false;
            if (resData.ResType == ResType.Asset)
            {
                GUI.Label(new Rect(x, y, WIDTH, HEIGHT),
                    $"Asset：{resData.AssetName}--{resData.RefCount}--{resData.LoadTime}", Style);
                return true;
            }
            else if (resData.ResType == ResType.AssetBundle)
            {
                GUI.Label(new Rect(x, y, WIDTH, HEIGHT), $"AB：{resData.ABName}--{resData.RefCount}", Style);
                return true;
            }

            return false;
        }
    }
}